<template>
  <view class="week">
    <view
      v-for="(item, i) in dates"
      class="week-item"
      @click="change(i)"
    >
      <view class="week-item-title"> {{ dayNames[i] }} </view>
      <view class="week-item-content">
        <view
          v-if="i === current"
          class="week-item-active"
        >
          <image :src="staticUrl('/static/images/widget/ink_round.png?r=sdfs')" />
        </view>
        <view
          class="week-item-text"
          :class="{
            'has-dot': hasDot(i),
            'is-active': i === current,
          }"
        >
          {{ item }}</view
        >
      </view>
    </view>
  </view>
</template>

<script>
  import moment from "moment";
  export default {
    props: {
      weekData: {
        type: Array,
        default: [],
      },
    },
    data() {
      return {
        start: moment().weekday(1).format("DD"), //本周一
        end: moment().weekday(7).format("DD"), //本周日
        dayNames: ["日", "一", "二", "三", "四", "五", "六"],
        dates: [],
        current: 0,
      };
    },

    mounted() {
      this.dates = this.getDates();
    },

    methods: {
      getDates() {
        let dates = [];
        // 取得今天
        const today = moment().format("DD");
        for (let i = 0; i < 7; i++) {
          const item = moment().weekday(i).format("DD");
          if (today === item) {
            this.current = i;
          }
          dates.push(item);
        }
        return dates;
      },
      hasDot(i) {
        const { weekData } = this;
        const date = moment().weekday(i).format("YYYY-MM-DD");
        const index = weekData.findIndex((item) => item.date === date);
        return index > -1;
      },

      change(i) {
        this.current = i;
        //根据item计算日期
        const date = moment().weekday(i).format("YYYY-MM-DD");
        console.log(date);
        this.$emit("change", date);
      },
    },
  };
</script>

<style lang="scss" scoped>
  .week {
    display: flex;
    justify-content: space-between;
    .week-item {
      display: flex;
      flex-direction: column;
      align-items: center;

      .week-item-title {
        font-family: "Songti SC";
      }
      .week-item-content {
        font-size: 32rpx;
        font-family: "Songti SC";
        font-weight: 900;
        display: flex;
        justify-content: center;
        align-items: center;
        position: relative;
        height: 100rpx;
      }
      &-text {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        font-family: "Songti SC";
        font-weight: 900;
        &.has-dot {
          &::after {
            content: "";
            position: absolute;
            left: 50%;
            bottom: -5rpx;
            width: 8rpx;
            height: 8rpx;
            border-radius: 50%;
            background-color: #000;
          }
        }

        &.is-active {
          color: #fff;
          &::after {
            background-color: #fff;
          }
        }
      }
      &-active {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        width: 50rpx * 2;
        height: 50rpx * 2;
        &::after {
          content: "";
          position: absolute;
          top: 50%;
          left: 50%;
          transform: translate(-50%, -50%);
          width: 60rpx;
          height: 60rpx;
          border-radius: 50%;
          background-color: #4f3e25;
        }
        image {
          width: 100%;
          height: 100%;
        }
      }
    }
  }
</style>
